package com.simple.puzzle;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Capture extends Activity implements OnClickListener {

	protected static final int CAPTURE_RESULT = 1;

	private final String LOG = "Capture";

	private Camera camera;
	private boolean inProgress = false;
	SurfaceView surfaceView;
	private Bitmap bitmap;

	private ImageView view;
	private ImageButton capture;
	private ImageButton back;
	/**
	 * surface callback
	 */
	private SurfaceHolder.Callback surfaceListener = new SurfaceHolder.Callback() {

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			camera.release();
			camera = null;
			Log.i(LOG, "camera release");
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			camera = Camera.open();
			Log.i(LOG, "camera opened");

			try {
				camera.setPreviewDisplay(holder);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
			Camera.Parameters parameters = camera.getParameters();
			parameters.setPreviewSize(width, height);
			camera.setParameters(parameters);
			camera.startPreview();
			Log.i(LOG, "camera preview started");

		}
	};

	/**
	 * shutter callback
	 */
	private Camera.ShutterCallback shutterListener = new Camera.ShutterCallback() {

		@Override
		public void onShutter() {
			// TODO Auto-generated method stub
			Log.i(LOG, "camera onshutter");
		}
	};

	/**
	 * picture callback
	 */
	private Camera.PictureCallback pictureListener = new Camera.PictureCallback() {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			Log.i(LOG, "picture taken");
			if (data != null) {
				Log.i("Size", String.valueOf(data.length));
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 4;
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
						options);

				view.setImageBitmap(bitmap);

				camera.startPreview();
				inProgress = false;

			}
		}
	};

	private void resumeGame() {
		if (bitmap != null) {
			Log.i(LOG, "resume game");
			Intent intent = new Intent();
			intent.putExtra("picture", bitmap);
			setResult(CAPTURE_RESULT, intent);
			finish();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capture);

		surfaceView = (SurfaceView) findViewById(R.id.surface);
		SurfaceHolder holder = surfaceView.getHolder();

		holder.addCallback(surfaceListener);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		surfaceView.setOnClickListener(this);

		back = (ImageButton) findViewById(R.id.back);
		capture = (ImageButton) findViewById(R.id.capture);
		view = (ImageView) findViewById(R.id.image_view);

		back.setOnClickListener(this);
		capture.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == surfaceView) {
			if (camera != null && inProgress == false) {
				camera.takePicture(shutterListener, null, pictureListener);
				inProgress = true;
			}
		}
		if (v == back) {
			resumeGame();
		}
	}

}
